import request, { all } from '@/utils/request'

/**
 * 借货人/货主/卖手列表
 * @param params
 * @returns AxiosPromise
 */
export function lendPaginate(params) {
  return request({
    url: '/statistics/lend/paginate',
    method: 'get',
    params: params
  })
}


/**
 * 供货订单统计
 * @param params
 */
export function getSupplyStatistics(params) {
  let select_field = [
    'order_total', 'goods_total', // 数量
    // 卖出价
    'sale_amount',
    'sale_final_price',
    'sale_purchase_price',
    'sale_courier_price',
    'sale_certificate_price',
    'sale_insurance_price',
    'sale_process_price',
    'sale_rope_price',
    'sale_package_price',
    'sale_other_price',
    // 实收款
    'real_amount',
    'real_final_price',
    'real_courier_price',
    'real_certificate_price',
    'real_insurance_price',
    'real_process_price',
    'real_rope_price',
    'real_package_price',
    'real_other_price',
    // 扣费
    'reduce_amount',
    'reduce_final_price',
    'reduce_courier_price',
    'reduce_certificate_price',
    'reduce_insurance_price',
    'reduce_process_price',
    'reduce_rope_price',
    'reduce_package_price',
    'reduce_other_price',
    'label_amount',
    'cost_amount',
    'cost_amount_origin',
    'unreturn_cost_amount_origin',
    'price_1_amount',
    'price_2_amount',
    'price_3_amount'
  ]
  const fieldMap = {
    unfinished: [
      'unfinished_count',
      'unfinished_sum',
      'unfinished_sale_amount'
    ],
    return_01: [
      'return_01_count',
      'return_01_sum',
      'return_01_sale_amount'
    ],
    return_02: [
      'return_02_count',
      'return_02_sum',
      'return_02_sale_amount'
    ],
    return_03: [
      'return_03_count',
      'return_03_sum',
      'return_03_sale_amount'
    ],
    return_04: [
      'return_04_count',
      'return_04_sum',
      'return_04_sale_amount'
    ],
    settle_02: [
      'settle_02_count',
      'settle_02_sum',
      'settle_02_sale_amount'
    ],
    settle_03: [
      'settle_03_count',
      'settle_03_sum',
      'settle_03_sale_amount'
    ]
  }
  fieldMap['return'] = [
    ...fieldMap['return_01'],
    ...fieldMap['return_02'],
    ...fieldMap['return_03'],
    ...fieldMap['return_04'],
    'return_total_count'
  ]
  fieldMap['return_un02'] = [
    ...fieldMap['return_01'],
    ...fieldMap['return_03'],
    ...fieldMap['return_04'],
    'return_total_count'
  ]
  fieldMap['wait_return'] = [
    ...fieldMap['unfinished'],
    ...fieldMap['return_02'],
    'return_total_count'
  ]
  fieldMap['settle'] = [
    ...fieldMap['settle_02'],
    ...fieldMap['settle_03'],
    'settle_total_count'
  ]
  fieldMap['unreturn'] = [
    ...fieldMap['unfinished'],
    ...fieldMap['settle_02'],
    ...fieldMap['settle_03'],
    'settle_total_count'
  ]
  fieldMap['settlement'] = [
    ...fieldMap['unfinished'],
    ...fieldMap['settle_02'],
    'settle_total_count'
  ]
  if (!params.filter) {
    select_field = select_field.concat([
      ...fieldMap['unfinished'],
      ...fieldMap['return_01'],
      ...fieldMap['return_02'],
      ...fieldMap['return_03'],
      ...fieldMap['return_04'],
      ...fieldMap['settle_02'],
      ...fieldMap['settle_03'],
      'settle_total_count',
      'return_total_count'
    ])
  } else if (fieldMap[params.filter]) {
    select_field = select_field.concat(fieldMap[params.filter])
  } else {
    console.error('未知的订单状态筛选', params.filter)
    return false
  }
  params = {
    ...params,
    select_field
  }
  return request({
    url: '/statistics/business/summary',
    method: 'post',
    data: params
  })
}

/**
 * 直播订单统计
 * @param params
 */
export function getLiveStatistics(params) {
  let select_field = [
    'order_total', 'goods_total', // 数量
    // 卖出价
    'sale_amount',
    'sale_final_price',
    'sale_purchase_price',
    'sale_courier_price',
    'sale_certificate_price',
    'sale_insurance_price',
    'sale_process_price',
    'sale_rope_price',
    'sale_package_price',
    'sale_other_price',
    // 结算价
    'settle_amount',
    'settle_final_price',
    'settle_courier_price',
    'settle_certificate_price',
    'settle_insurance_price',
    'settle_process_price',
    'settle_rope_price',
    'settle_package_price',
    'settle_other_price',
    // 扣费
    'reduce_amount',
    'reduce_final_price',
    'reduce_courier_price',
    'reduce_certificate_price',
    'reduce_insurance_price',
    'reduce_process_price',
    'reduce_rope_price',
    'reduce_package_price',
    'reduce_other_price',
    'cost_amount',
    'cost_amount_origin',
    'price_1_amount',
    'price_2_amount',
    'price_3_amount'
  ]
  const fieldMap = {
    unfinished: [
      'unfinished_count',
      'unfinished_sum',
      'unfinished_sale_amount'
    ],
    return_01: [
      'return_01_count',
      'return_01_sum',
      'return_01_sale_amount'
    ],
    return_02: [
      'return_02_count',
      'return_02_sum',
      'return_02_sale_amount'
    ],
    return_03: [
      'return_03_count',
      'return_03_sum',
      'return_03_sale_amount'
    ],
    return_04: [
      'return_04_count',
      'return_04_sum',
      'return_04_sale_amount'
    ],
    settle_01: [
      'settle_01_count',
      'settle_01_sum',
      'settle_01_sale_amount'
    ],
    settle_02: [
      'settle_02_count',
      'settle_02_sum',
      'settle_02_sale_amount'
    ]
  }
  fieldMap['return'] = [
    ...fieldMap['return_01'],
    ...fieldMap['return_02'],
    ...fieldMap['return_03'],
    ...fieldMap['return_04'],
    'return_total_count'
  ]
  fieldMap['return_un02'] = [
    ...fieldMap['return_01'],
    ...fieldMap['return_03'],
    ...fieldMap['return_04'],
    'return_total_count'
  ]
  fieldMap['wait_return'] = [
    ...fieldMap['unfinished'],
    ...fieldMap['return_02'],
    'return_total_count'
  ]
  fieldMap['settle'] = [
    ...fieldMap['settle_01'],
    ...fieldMap['settle_02'],
    'settle_total_count'
  ]
  fieldMap['unreturn'] = [
    ...fieldMap['unfinished'],
    ...fieldMap['settle_01'],
    ...fieldMap['settle_02'],
    'settle_total_count'
  ]
  fieldMap['unsettle'] = [
    ...fieldMap['unfinished'],
    ...fieldMap['settle_01'],
    'settle_total_count'
  ]
  if (!params.filter) {
    select_field = select_field.concat([
      ...fieldMap['unfinished'],
      ...fieldMap['return_01'],
      ...fieldMap['return_02'],
      ...fieldMap['return_03'],
      ...fieldMap['return_04'],
      ...fieldMap['settle_01'],
      ...fieldMap['settle_02'],
      'settle_total_count',
      'return_total_count'
    ])
  } else if (fieldMap[params.filter]) {
    select_field = select_field.concat(fieldMap[params.filter])
  } else {
    console.error('未知的订单状态筛选', params.filter)
    return false
  }
  params = {
    ...params,
    select_field
  }
  return request({
    url: '/statistics/business/summary',
    method: 'post',
    data: params
  })
}
